home *** CD-ROM | disk | FTP | other *** search
/ Light ROM 1 / LIGHT-ROM 1 (Amiga Library Services)(1994).iso / ffdisks / d886.lha / TrackEd / Sources / ReadBlock.c < prev    next >
C/C++ Source or Header  |  1993-07-16  |  2KB  |  109 lines

  1.  
  2. /* ReadBlock.c  Routines for reading and writing blocks */
  3.  
  4. #include <exec/io.h>
  5. #include <devices/trackdisk.h>
  6.  
  7. struct IOStdReq *OpenTDIO(unit)
  8. UBYTE unit;
  9. {
  10.    struct IOStdReq *diskio;
  11.    struct MsgPort *readreply;
  12.    int error = -1;
  13.  
  14.    if (readreply = (void *) CreatePort(0, 0))
  15.    {
  16.       if (diskio = (void *) CreateStdIO(readreply))
  17.       {
  18.          error = OpenDevice(TD_NAME, unit, diskio, 0);
  19.       }
  20.    }
  21.    if (error == 0) return(diskio);
  22.    else return(NULL);
  23. }
  24.  
  25. int ReadSBlock(io, first_block, blocks, buffer)
  26. struct IOStdReq *io;
  27. WORD first_block, blocks;
  28. UBYTE *buffer;
  29. {
  30.    io->io_Command = CMD_READ;
  31.    io->io_Data = buffer;
  32.    io->io_Length = blocks*512L;
  33.    io->io_Offset = first_block*512L;
  34.    DoIO(io);
  35.    return((int) io->io_Error);
  36. }
  37.  
  38. int WriteSBlock(io, first_block, blocks, buffer)
  39. struct IOStdReq *io;
  40. WORD first_block, blocks;
  41. UBYTE *buffer;
  42. {
  43.    int error;
  44.  
  45.    io->io_Command = CMD_WRITE;
  46.    io->io_Data = buffer;
  47.    io->io_Length = blocks*512L;
  48.    io->io_Offset = first_block*512L;
  49.    DoIO(io);
  50.    error = io->io_Error;
  51.  
  52.    io->io_Command = CMD_UPDATE;
  53.    DoIO(io);
  54.    if (!error) error = io->io_Error;
  55.    return(error);
  56. }
  57.  
  58. void MotorOff(io)
  59. struct IOStdReq *io;
  60. {
  61.    io->io_Command = TD_MOTOR;
  62.    io->io_Length = 0L;
  63.    DoIO(io);
  64. }
  65.  
  66. void CloseTDIO(io)
  67. struct IOStdReq *io;
  68. {
  69.    struct MsgPort *ReplyPort;
  70.  
  71.    if (io)
  72.    {
  73.       ReplyPort = io->io_Message.mn_ReplyPort;
  74.       CloseDevice(io);
  75.       DeleteStdIO(io);
  76.       if (ReplyPort) DeletePort(ReplyPort);
  77.    }
  78. }
  79.  
  80. int ReadBlocks(unit, first_block, blocks, buffer)
  81. UBYTE unit;
  82. WORD first_block, blocks;
  83. UBYTE *buffer;
  84. {
  85.    struct IOStdReq *io;
  86.    int error;
  87.  
  88.    io = OpenTDIO(unit);
  89.    error = ReadSBlock(io, first_block, blocks, buffer);
  90.    MotorOff(io);
  91.    CloseTDIO(io);
  92.    return(error);
  93. }
  94.  
  95. int WriteBlocks(unit, first_block, blocks, buffer)
  96. UBYTE unit;
  97. WORD first_block, blocks;
  98. UBYTE *buffer;
  99. {
  100.    struct IOStdReq *io;
  101.    int error;
  102.  
  103.    io = OpenTDIO(unit);
  104.    error = WriteSBlock(io, first_block, blocks, buffer);
  105.    MotorOff(io);
  106.    CloseTDIO(io);
  107.    return(error);
  108. }
  109.